<?php
if(!defined('BZ_XTBLOG') || !defined('BZ_WJSCUZ')) {
	exit('Access Denied');
}
########################################
//qq登陆
function do_post($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_POST, TRUE); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt($ch, CURLOPT_URL, $url);
    $ret = curl_exec($ch);
    curl_close($ch);
    return $ret;
}
function do_explode($data, $name)
{
	$data = explode('&',$data);
	$a = array();
	foreach ($data as $v ){
		$s = explode('=',$v);
		$a[$s[0]]=$s[1];
	}
	unset($data);
	if( isset($a[$name]) ){
		return $a[$name];
	}
}
function do_callback($data, $name)
{
	preg_match("/\{(.*?)}/",$data,$link);
	$data=str_replace('"','',$link[1]);
	$data = explode(',',$data);
	$a = array();
	foreach ($data as $v ){
		$s = explode(':',$v);
		$a[$s[0]]=$s[1];
	}
	unset($data);
	if( isset($a[$name]) ){
		return $a[$name];
	}
}

########################################
$_SESSION["appid"]    = Bingz_config('user_config_qqlogin_appid'); //申请到的appkey
$_SESSION["appkey"]   = Bingz_config('user_config_qqlogin_appkey'); //QQ登录成功后跳转的地址,请确保地址真实可用，否则会导致登录失败。
$_SESSION["callback"] = Bingz_http(Bingz_config('user_config_qqlogin_url')).'/index.php?m=user&mod=login_qq'; //QQ登录成功后跳转的地址,请确保地址真实可用，否则会导致登录失败
$_SESSION["scope"] = Bingz_config('user_config_qqlogin_api');//QQ授权api接口.按需调用

if($_GET['act'] == 'login'){//用户点击qq登录
function qq_login($appid, $scope, $callback)
{
    $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection  https://graph.qq.com/oauth2.0/authorize
    $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" 
        . $appid . "&redirect_uri=" . urlencode($callback)
        . "&state=" . $_SESSION['state']
        . "&scope=".$scope;
    header("Location:$login_url");
}
qq_login($_SESSION["appid"], $_SESSION["scope"], $_SESSION["callback"]);//用户点击qq登录按钮调用此函数
###############################
}elseif($_GET['act'] == 'loginok'){//创建账号
$ip_dz=ip_address($ip);
	    $pass=user_mm('123456');
      $pwss=md5($time);
      $user_config_reg_xinxi=Bingz_config('user_config_reg_xinxi');
      $user_config_reg_money=Bingz_config('user_config_reg_money');
      $user_config_reg_jifen=Bingz_config('user_config_reg_jifen');
      $user_config_reg_yeobi=Bingz_config('user_config_reg_yeobi');
      if($qquser["gender"] == '男'){$sex='2';}else{$sex='1';}
      $nickname=$qquser[nickname];
  if($db->insert('user',array('uid' => 'NULL', 'sex' => $_COOKIE['user_sex'],'name' => $_COOKIE['user_name'],'nameys' => '#000000', 'qq_access_token' => $_COOKIE['user_access_token'],'qq_openid' => $_COOKIE['user_openid'],'email' => $email, 'mobile' => $phone, 'pass'=> $pass, 'pwss'=> $pwss,  'yeobi'=> $user_config_reg_yeobi,'money'=> $user_config_reg_money, 'jifen'=> $user_config_reg_jifen,'time_a'=> $time, 'time_b'=> $time,'ip' => $ip ,'ip_dz' => $ip_dz, 'ip_at'=> $ip,'show' => $_COOKIE['user_logo'],'user_zx' => '5' ))){
  	 $uid=$db->insert_id();
	   setcookie('user_login',$pwss);
	   setcookie('user_ip',$ip);
  	 Bingz_message_post('0',$uid,$user_config_reg_xinxi);
		 $db->insert('user_dl',array('id' => 'NULL', 'type' => 0, 'uid' => $uid, 'uip' => $ip, 'uip_dz'=> $ip_dz , 'zhuangt'=> '成功' , 'time_a'=> $time));	
  }
	 setcookie('user_uid',$uid);
	 setcookie('user_name',$nickname);
	 setcookie('user_sex',$sex);
	 setcookie('user_access_token',$access_token);
	 setcookie('user_openid',$openid);
   header("Location:index.php?$Bingz_ml=user");
}elseif($_GET['act'] == 'loginbd'){//账号绑定
$wzxx_title ='qq登陆-'.$wzxx_title.'';
$wzxx_gj    ='qq登陆-'.$wzxx_gj.'';
$wzxx_ms    ='qq登陆-'.$wzxx_ms.'';
       $dluid=$user_class->user_login($_POST[name],$_POST[pass]);
	     if($dluid > '0' ){
	     	  $db->update('user',array('qq_access_token' => $_COOKIE['user_access_token'],'qq_openid' => $_COOKIE['user_openid']),"`uid` ='$dluid'");
	     	  $mts='绑定成功！';
	     	  $pdid=1;
	 setcookie('user_uid',$dluid);
	 setcookie('user_name',$nickname);
	 setcookie('user_sex',$sex);
	 setcookie('user_access_token',$access_token);
	 setcookie('user_openid',$openid);
	     }else{
	     	  $nickname=$_COOKIE['user_name'];
	     	  $mts='账号错误！';
	     	  $pdid=2;
	     }
@include template('head');
@include template('user/user_login_qq');
@include template('foot');
}else{
    $accessurl  = "https://graph.qq.com/oauth2.0/token";
    $accessdata = 'grant_type=authorization_code&client_id='.$_SESSION["appid"].'&client_secret='.$_SESSION["appkey"].'&code='.$_GET['code'].'&redirect_uri='.$_SESSION["callback"].'';
    $access =  do_post($accessurl, $accessdata); 
    $access_token=do_explode($access, 'access_token');
    
    $url  = "https://graph.qq.com/oauth2.0/me";
    $data = 'access_token='.$access_token.'';
    $ret =  do_post($url, $data); 
    $openid=do_callback($ret, 'openid');

    $_SESSION['access_token']=$access_token;
    $_SESSION["openid"]=$openid;
function get_user_info()
{
    $get_user_info = "https://graph.qq.com/user/get_user_info?"
        . "access_token=".$_SESSION['access_token']
        . "&oauth_consumer_key=".$_SESSION["appid"]
        . "&openid=".$_SESSION["openid"]
        . "&format=json";
    $info = file_get_contents($get_user_info);
    $arr = json_decode($info, true);
    return $arr;
}
$qquser = get_user_info();//获取用户基本资料
$nickname=$qquser["nickname"];
if($usoo=$db->get_one('user'," `qq_openid` = '$openid' ",'')){
	 $db->update('user',array('qq_access_token' => $access_token,'user_zx' => 5 , 'time_a' => $time,'ip' => $ip ,'ip_dz' => $ip_dz),"`uid` ='$usoo[uid]'");
	 $db->insert('user_dl',array('id' => 'NULL', 'type' => 0, 'uid' => $usoo[uid], 'uip' => $ip, 'uip_dz'=> $ip_dz , 'zhuangt'=> '成功' , 'time_a'=> $time));	
	 setcookie('user_login',$usoo[pwss]);
	 setcookie('user_ip',$ip);
	 setcookie('user_logo',$qquser[figureurl_2]);
	 setcookie('user_uid',$usoo[uid]);
   header("Location:index.php?$Bingz_ml=user");
}else{
	 setcookie('user_logo',$qquser[figureurl_2]);
	 setcookie('user_name',$qquser[nickname]);
	 setcookie('user_sex',$sex);
	 setcookie('user_access_token',$access_token);
	 setcookie('user_openid',$openid);

$wzxx_title ='qq登陆-'.$wzxx_title.'';
$wzxx_gj    ='qq登陆-'.$wzxx_gj.'';
$wzxx_ms    ='qq登陆-'.$wzxx_ms.'';
@include template('head');
@include template('user/user_login_qq');
@include template('foot');
}
}
?>